package site.wanjiahao.handler.server;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import site.wanjiahao.message.request.ChartRequestMessage;
import site.wanjiahao.message.response.ChartResponseMessage;
import site.wanjiahao.session.SessionFactory;

@ChannelHandler.Sharable
public class ChartRequestMessageHandler extends SimpleChannelInboundHandler<ChartRequestMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ChartRequestMessage msg) throws Exception {
        // 找到在线用户
        Channel channel = SessionFactory.getSession().getChannel(msg.getTo());
        if (channel != null) {
            channel.writeAndFlush(new ChartResponseMessage(SessionFactory.getSession().getUsername(ctx.channel()), msg.getContent()));
        } else {
            ctx.writeAndFlush(new ChartResponseMessage(false, "不存在该用户"));
        }
    }
}
